package com.medicine.manage.service.impl;

import java.util.List;
import com.medicine.common.utils.DateUtils;
import com.medicine.common.utils.SecurityUtils;
import com.medicine.manage.block.service.IBlockOrderProcessService;
import com.medicine.manage.domain.Order;
import com.medicine.manage.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.medicine.manage.mapper.OrderProcessMapper;
import com.medicine.manage.domain.OrderProcess;
import com.medicine.manage.service.IOrderProcessService;

/**
 * 流程Service业务层处理
 *
 * @author admin
 * @date 2024-10-13
 */
@Service
public class OrderProcessServiceImpl implements IOrderProcessService
{
    @Autowired
    private OrderProcessMapper orderProcessMapper;

    @Autowired
    private IBlockOrderProcessService blockOrderProcessService;

    @Autowired
    private IOrderService orderService;

    /**
     * 查询流程
     *
     * @param id 流程主键
     * @return 流程
     */
    @Override
    public OrderProcess selectOrderProcessById(Long id)
    {
        return orderProcessMapper.selectOrderProcessById(id);
    }

    /**
     * 查询流程列表
     *
     * @param orderProcess 流程
     * @return 流程
     */
    @Override
    public List<OrderProcess> selectOrderProcessList(OrderProcess orderProcess)
    {
        return orderProcessMapper.selectOrderProcessList(orderProcess);
    }

    /**
     * 新增流程
     *
     * @param orderProcess 流程
     * @return 结果
     */
    @Override
    public int insertOrderProcess(OrderProcess orderProcess)
    {
        orderProcess.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        orderProcess.setCreateTime(DateUtils.getNowDate());
        int result = orderProcessMapper.insertOrderProcess(orderProcess);
        blockOrderProcessService.insertOrderProcess(orderProcess, SecurityUtils.getLoginUser().getDeptId() , SecurityUtils.getLoginUser().getUserId());
        return result;
    }

    /**
     * 修改流程
     *
     * @param orderProcess 流程
     * @return 结果
     */
    @Override
    public int updateOrderProcess(OrderProcess orderProcess)
    {
        orderProcess.setUpdateBy(SecurityUtils.getLoginUser().getUserId().toString());
        orderProcess.setUpdateTime(DateUtils.getNowDate());

        int result = orderProcessMapper.updateOrderProcess(orderProcess);

        OrderProcess process = orderProcessMapper.selectOrderProcessById(orderProcess.getId());

        if(process.getStatus() == 1L){
            Order order = orderService.selectOrderById(orderProcess.getOrderId());
            blockOrderProcessService.updateOrderProcess(process, order.getDeptId() , new Long(orderProcess.getCreateBy()));
        } else {
            blockOrderProcessService.updateOrderProcess(process, SecurityUtils.getLoginUser().getDeptId() , SecurityUtils.getLoginUser().getUserId());
        }


        return result;

    }


    /**
     * 修改流程
     *
     * @param orderProcess 流程
     * @return 结果
     */
    @Override
    public int updateProcessStatusByOrderAndType(OrderProcess orderProcess)
    {
        orderProcess.setUpdateBy(SecurityUtils.getLoginUser().getUserId().toString());
        orderProcess.setUpdateTime(DateUtils.getNowDate());
        System.out.println(orderProcess);
        int result = orderProcessMapper.updateProcessStatusByOrderAndType(orderProcess);


        List<OrderProcess> processes = orderProcessMapper.selectOrderProcessList(orderProcess);
        if(!processes.isEmpty()){
            OrderProcess process = processes.get(0);
            blockOrderProcessService.updateOrderProcess(process, SecurityUtils.getLoginUser().getDeptId() , SecurityUtils.getLoginUser().getUserId());

        } else {
            throw new RuntimeException("不存在的process, orderId:" + orderProcess.getOrderId() + "  ,processType:" + orderProcess.getProcessType());
        }
        return result;

    }



    /**
     * 批量删除流程
     *
     * @param ids 需要删除的流程主键
     * @return 结果
     */
    @Override
    public int deleteOrderProcessByIds(Long[] ids)
    {
        return orderProcessMapper.deleteOrderProcessByIds(ids);
    }

    /**
     * 删除流程信息
     *
     * @param id 流程主键
     * @return 结果
     */
    @Override
    public int deleteOrderProcessById(Long id)
    {
        return orderProcessMapper.deleteOrderProcessById(id);
    }

    @Override
    public List<OrderProcess> selectOrderProcessListAndDetail(OrderProcess orderProcess) {
        return orderProcessMapper.selectOrderProcessListAndDetail(orderProcess);
    }

    @Override
    public OrderProcess selectOrderProcessByOrderAndType(Long orderId, Long type) {
        return orderProcessMapper.selectOrderProcessByOrderAndType(orderId, type);
    }
}
